package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.TagEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.interestwall.TagChooseFragment.onTagChooseListener;
import com.yksj.healthtalk.ui.interestwall.TagShowFragment.OnTagShowItemClickListener;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 标签选择activity
 * @author crj
 *
 */
public class TagChooseActivity extends BaseFragmentActivity implements
		OnTagShowItemClickListener, onTagChooseListener, OnClickListener {

	private Intent intent = null;
	/**
	 * xml文件id
	 */
	private int resid;
	private ViewPager mPager;
	/**
	 *  TagShowFragment 放置的适配器，当添加个数超过15个时自动添加一个fragment
	 */
	private TagFragmentStateAdapter adapter;
	private FragmentManager manager;
	/**
	 * 标签选择fragment
	 */
	private TagChooseFragment mChooseFragment;
	private ArrayList<TagEntity> tags;
	private ArrayList<TagEntity> tagsCopy;
	private ArrayList<ArrayList<TagEntity>> metadata;
	//滑屏数
	private int pageCount;
	/**
	 * 修改前 metadata 长度
	 */
	private int oldListLength = -1;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tag_choose_layout);
		intent = getIntent();
		resid = intent.getIntExtra("resid", -1);
		tags = intent.getParcelableArrayListExtra("tags");
		if(tags!=null) {
			tagsCopy = new ArrayList<TagEntity>();
			tagsCopy.addAll(tags);
		}
		if (resid == -1) {
			ToastUtil.showShort(this, "值传递失败");
		}
		initData();
		initView();
	}

	public void initData() {
		if(tags==null)
			tags = new ArrayList<TagEntity>();
		if(tagsCopy==null)
		tagsCopy = new ArrayList<TagEntity>();
		manager = getSupportFragmentManager();
		metadata = convertData(tags);
		adapter = new TagFragmentStateAdapter(manager);
	}

	public void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleTextV.setText("标签选择");
		
		
		mPager = (ViewPager) findViewById(R.id.interest_pager);
		mPager.setAdapter(adapter);
		mChooseFragment = TagChooseFragment.getInstance(resid);
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.fragmentcontent, mChooseFragment);
		transaction.commit();
		mChooseFragment.setOnTagChooseListener(this);
		mChooseFragment.notifyDataInit(tagsCopy);
	}

	public ArrayList<ArrayList<TagEntity>> convertData(ArrayList<TagEntity> data) {
		ArrayList<ArrayList<TagEntity>> resultData = new ArrayList<ArrayList<TagEntity>>();
		pageCount = data.size() / 15;
		if (data.size() % 15 != 0) {
			pageCount = data.size() / 15 + 1;
		}
		while (resultData.size() < pageCount) {
			ArrayList<TagEntity> tempList = new ArrayList<TagEntity>();
			if (resultData.size() == pageCount - 1) {
				for (int i = 0; i < data.size(); i++) {
					tempList.add(data.get(i));
				}
				resultData.add(tempList);
				data.removeAll(tempList);
			}
			if (resultData.size() < pageCount - 1) {
				for (int i = 0; i < 15; i++) {
					tempList.add(data.get(i));
				}
				resultData.add(tempList);
				data.removeAll(tempList);
			}
		}
		return resultData;
	}

	/**
	 * tagshowfragment 适配器，用viewpager实现左右划屏
	 * @author Administrator
	 *
	 */
	public class TagFragmentStateAdapter extends FragmentStatePagerAdapter {

		private List<TagShowFragment> fragments;

		public TagFragmentStateAdapter(FragmentManager fm) {
			super(fm);
			fragments = new ArrayList<TagShowFragment>();
		}

		@Override
		public TagShowFragment getItem(int arg0) {
			TagShowFragment mFragment = TagShowFragment.newInstance(arg0,
					metadata.get(arg0));
			if (fragments.size() > arg0) {
				fragments.set(arg0, mFragment);
			} else {
				fragments.add(mFragment);
			}
			mFragment.setOnTagShowItemClickListener(TagChooseActivity.this);
			return mFragment;
		}

		@Override
		public int getCount() {
			return metadata.size();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
		}

		@Override
		public void notifyDataSetChanged() {
			super.notifyDataSetChanged();
			if (fragments.size() > 0 && metadata.size() > 0) {
				if (metadata.size() >= fragments.size()) {
					for (int i = 0; i < fragments.size() ; i++) {
						fragments.get(i).notifyGridView(
								metadata.get(i));
					}
				} else {
					for(int i=0;i<metadata.size();i++) {
						fragments.get(i).notifyGridView(
								metadata.get(i));
					}
					fragments.get(fragments.size() - 1).notifyGridView(
							new ArrayList<TagEntity>());
				}
			}

			if (metadata.size() == 0) {
				for (int i = 0; i < fragments.size() ; i++) {
					fragments.get(i).notifyGridView(
							new ArrayList<TagEntity>());
				}
			}
		}

	}

	@Override
	public void onTagClick(int gridviewposition, int pageposition,
			TagEntity itemData) {
		tags.addAll(tagsCopy);
		tags.remove(itemData);
		tagsCopy.clear();
		tagsCopy.addAll(tags);
		metadata = convertData(tags);
		adapter.notifyDataSetChanged();
		
		mChooseFragment.notifyDataChang(itemData.getId());
		//确定切换到第几页
		if(oldListLength > metadata.size()) {
			mPager.setCurrentItem(metadata.size()-1, true);
			oldListLength  = metadata.size();
		} else {
			mPager.setCurrentItem(pageposition, true);
		}
	}

	@Override
	public void tagChoose(TagEntity entity, int tag) {
		tags.addAll(tagsCopy);
		if (tag == 1) {
			if(!tags.contains(entity))
				tags.add(entity);
		}
		if (tag == 0) {
			tags.remove(entity);
		}
		tagsCopy.clear();
		tagsCopy.addAll(tags);
		metadata = convertData(tags);
		adapter.notifyDataSetChanged();
		mPager.setCurrentItem(metadata.size() - 1, true);
		oldListLength  = metadata.size();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			this.finish();
			break;
		case R.id.title_right2:
			if(tagsCopy.size()>0) {
				Intent intent = new Intent();
				intent.putParcelableArrayListExtra("tags", tagsCopy);
				setResult(RESULT_OK, intent);
				this.finish();
			} else {
				ToastUtil.showLong(this, "标签不能为空");
			}
			break;
		}
	}
}
